package com.llmcu.graphic.designpatterns.interpreter;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;
import java.util.StringTokenizer;

@Setter
@Getter
@AllArgsConstructor
public class Context {
    private String text;
    private StringTokenizer stringTokenizer;
    private String currentToken;

    public Context(String text) {
        this.text = text;
        stringTokenizer= new StringTokenizer(text);
        nextToken();
    }

    public void skip(String expectedToken){
        if (!Objects.equals(expectedToken, getCurrentToken())) {
            throw new RuntimeException(expectedToken + " token expected, but "+getCurrentToken()+" found");
        }
        nextToken();
    }

    public String nextToken(){
        if(stringTokenizer.hasMoreTokens()){
            String nextToken = stringTokenizer.nextToken();
            currentToken = nextToken;
            return nextToken;
        }
        return null;
    }

}
